package com.macro.mall.tiny.modules.pms.controller;

import com.google.common.collect.Lists;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsCoolTool;
import com.macro.mall.tiny.modules.pms.model.PmsElectricMasterData;
import com.macro.mall.tiny.modules.pms.model.PmsRepairData;
import com.macro.mall.tiny.modules.pms.service.PmsCoolToolService;
import com.macro.mall.tiny.modules.pms.service.PmsElectricMasterDataService;
import com.macro.mall.tiny.modules.pms.service.PmsRepairDataService;
import com.macro.mall.tiny.modules.pms.service.PmsUserActionService;
import com.macro.mall.tiny.modules.ums.model.UmsMember;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Tyrone
 * @date 2024/11/7 00:54
 */
@RestController
@Api(tags = "PmsCollectItemController")
@Tag(name = "PmsCollectItemController", description = "收藏管理")
@RequestMapping("/pms/pmsCollect")
public class PmsCollectItemController {

    @Autowired
    private PmsUserActionService pmsUserActionService;

    @Autowired
    private PmsElectricMasterDataService electricMasterDataService;
    @Autowired
    private PmsCoolToolService pmsCoolToolService;
    @Autowired
    private PmsRepairDataService pmsRepairDataService;

    @ApiOperation("获取收藏的电工大师数据")
    @GetMapping("/getCollectElectricMasterData")
    public CommonResult<List<PmsElectricMasterData>> getCollectElectricMasterData() {
        List<Integer> collected = pmsUserActionService.listItem(2, 1).stream().map(e -> (Integer) e).collect(Collectors.toList());
        if (collected.isEmpty()) {
            return CommonResult.success(Lists.newArrayList());
        }
        List<PmsElectricMasterData> pmsElectricMasterData = electricMasterDataService.listByIds(collected);
        pmsElectricMasterData.stream().forEach(e -> e.setCurrentMemberCollect(1));
        return CommonResult.success(pmsElectricMasterData);
    }

    @ApiOperation("获取收藏的维修数据")
    @GetMapping("/getCollectRepairData")
    public CommonResult<List<PmsRepairData>> getCollectRepairData() {
        List<Integer> collected = pmsUserActionService.listItem(3, 1).stream().map(e -> (Integer) e).collect(Collectors.toList());
        if (collected.isEmpty()) {
            return CommonResult.success(Lists.newArrayList());
        }
        List<PmsRepairData> pmsRepairData = pmsRepairDataService.listByIds(collected);

        return CommonResult.success(pmsRepairData);
    }

    @ApiOperation("获取收藏的中央空调数据")
    @GetMapping("/getCollectCentralData")
    public CommonResult<List<PmsCoolTool>> getCollectCentralData() {
        List<Integer> collected = pmsUserActionService.listItem(4, 1).stream().map(e -> (Integer) e).collect(Collectors.toList());
        if (collected.isEmpty()) {
            return CommonResult.success(Lists.newArrayList());
        }
        List<PmsCoolTool> pmsCoolTools = pmsCoolToolService.listByIds(collected);

        return CommonResult.success(pmsCoolTools);
    }



    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("获取最近搜索记录")
    @GetMapping("/getRecentSearch")
    public CommonResult<List<String>> getRecentSearch(@ApiParam("0 制冷云 1电工大师 2维修数据") @RequestParam Integer type) {
        UmsMember member = MemberContext.getMember();
        if (member == null) {
            return CommonResult.success(new ArrayList<>());
        }
        String pmsCoolToolKey = "PmsCoolTool:" + member.getId();
        String pmsElectricMasterDataKey = "PmsElectricMasterData:" + member.getId();
        if (type == 0) {
            return CommonResult.success(redisTemplate.opsForList().range(pmsCoolToolKey, 0, -1));
        }
        if (type == 1) {
            return CommonResult.success(redisTemplate.opsForList().range(pmsElectricMasterDataKey, 0, -1));
        }
        if (type == 2){
            return CommonResult.success(redisTemplate.opsForList().range("PmsRepairData:" + member.getId(), 0, -1));
        }
        return CommonResult.success(null);
    }

    @ApiOperation("清空最近搜索记录")
    @DeleteMapping("/clearRecentSearch")
    public CommonResult<Void> clearRecentSearch(@ApiParam("0 制冷云 1电工大师") @RequestParam Integer type) {
        UmsMember member = MemberContext.getMember();
        if (member == null) {
            return CommonResult.success(null);
        }
        if (type == 0) {
            redisTemplate.delete("PmsCoolTool:" + member.getId());
        }
        if (type == 1) {
            redisTemplate.delete("PmsElectricMasterData:" + member.getId());
        }
        if (type == 2) {
            redisTemplate.delete("PmsRepairData:" + member.getId());
        }
        return CommonResult.success(null);
    }

    @ApiOperation("删除单个搜索记录")
    @DeleteMapping("/deleteSingleSearch")
    public CommonResult<Void> deleteSingleSearch(@ApiParam("0 制冷云 1电工大师") @RequestParam Integer type, @ApiParam("搜索关键字") @RequestParam String keyword) {
        UmsMember member = MemberContext.getMember();
        if (member == null) {
            return CommonResult.success(null);
        }
        if (type == 0) {
            redisTemplate.opsForList().remove("PmsCoolTool:" + member.getId(), 0, keyword);
        }
        if (type == 1) {
            redisTemplate.opsForList().remove("PmsElectricMasterData:" + member.getId(), 0, keyword);
        }
        if (type == 2) {
            redisTemplate.opsForList().remove("PmsRepairData:" + member.getId(), 0, keyword);
        }
        return CommonResult.success(null);
    }

}
